﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Contract;

public partial class Ctl_ctlPaymentsPlan : System.Web.UI.UserControl
{

    public Func<EntityList> DataSource;
    public Func<bool> OnChangeDataSource;
    public Type TypeOfPayents { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MyDataBind();
    }

    private void MyDataBind()
    {        
        if (IsSourceExists )
        {
            DataSource().Reset();
            lvList.DataSource = DataSource();         
        }
        lvList.DataBind();
    }
    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        MyDataBind();
    }

    private bool IsSourceExists
    {
        get
        {
            if (null == DataSource)
                return false;

            if (null == DataSource())
                return false;

            return true;
        }
    }

    protected void btnRemoveClick(object sender, ImageClickEventArgs e)
    {
        if (!IsSourceExists)
            return;
        string RowId = (sender as ImageButton).CommandArgument;        
        DataSource().Remove(RowId);
        MyDataBind();

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!IsSourceExists)
            return;
        TPaymentItem item = null ;
        if (TypeOfPayents.Equals (typeof (TContractPaymentItem_Plan)))

            item = new TContractPaymentItem_Plan();
        else
            item = new TContractPaymentItem_Payables();     


        DataSource ().Add (item );        
        MyDataBind();
    }

    protected bool  UpdateRow()
    {
        if (null == lvList.EditItem)
            return false ;

        try
        {
            TextBox t = (TextBox)lvList.EditItem.FindControl("txtDate");
            HiddenField hf = (HiddenField)lvList.EditItem.FindControl("hfCurrentRow");
            (DataSource()[hf.Value] as TPaymentItem).Date = Convert.ToDateTime(t.Text);
            t = (TextBox)lvList.EditItem.FindControl("txtSumma");
            (DataSource()[hf.Value] as TPaymentItem).Summa = Convert.ToInt16 (t.Text);
            
            if (null != OnChangeDataSource)
                return OnChangeDataSource() ;

            return false;
        }
        catch
        { return false; }

    }

    protected void lvList_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
           if (string.Empty == e.CommandName) return;

        ListView lv = (ListView)sender;

        

        switch (e.CommandName)
        {
            case "Edit":
                {
                        ListViewDataItem d = (ListViewDataItem)e.Item;
                        lv.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    if (UpdateRow())
                        lv.EditIndex = -1;
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;

                }
        }

        MyDataBind();

    }
    protected void lvList_ItemEditing(object sender, ListViewEditEventArgs e)
    {

    }
    protected void lvList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }
}
